package org.abettor.pushbox.upload;

/**
 * 
 */
import java.util.HashMap;
import java.util.Map;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.net.NetTools;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;

public class SendFlowerOrEggUpload {
	/**
	 * 更新详情的接口
	 * 
	 * @author jzh
	 * 
	 */
	public static interface UpdateShowMapDetail {
		/**
		 * 更新的数量
		 * 
		 * @param count
		 */
		public void update(int count);
	}

	private Handler mHandler = new Handler();
	private NetTools netTools = new NetTools();
	private UpdateShowMapDetail callBack;// 进行回调的方法
	private Activity context;
	private ProgressDialog progress;// 进度条对话框

	public SendFlowerOrEggUpload(Activity activity) {
		context = activity;
	}

	/**
	 * 送鲜花的操作
	 * 
	 * @return
	 */
	public void postFlower(UpdateShowMapDetail call,final int mapId) {
		callBack = call;
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.showmap_detail_activity_msg);
		progress.setMessage(context
				.getText(R.string.showmap_detail_activity_sendflowering));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> map = new HashMap<String, String>();
				map.put("mapId", String.valueOf(mapId));
				String ret = netTools.postData(Config.markFlower, map);
				progress.dismiss();
				if (ret == null) {
					showFlowerFailDialog();// 送鲜花失败
					return;
				}
				int value = ParaUploadRetValue.paraSendFlowerCount(ret);
				if (value == 0) {
					showFlowerFailDialog();// 送鲜花失败
					return;
				}
				showFlowerSuccessDialog(value);
			}
		};
		thread.start();
	}

	/**
	 * 送鸡蛋的操作
	 * 
	 * @return
	 */
	public void postEgg(UpdateShowMapDetail call,final int mapId) {
		callBack = call;
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.showmap_detail_activity_msg);
		progress.setMessage(context
				.getText(R.string.showmap_detail_activity_sendegging));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> map = new HashMap<String, String>();
				map.put("mapId", String.valueOf(mapId));
				String ret = netTools.postData(Config.markEgg, map);
				progress.dismiss();
				if (ret == null) {
					showFlowerFailDialog();// 送鲜花失败
					return;
				}
				int value = ParaUploadRetValue.paraSendEggCount(ret);
				if (value == 0) {
					showFlowerFailDialog();// 送鲜花失败
					return;
				}
				showSendEggSuccessDialog(value);
			}
		};
		thread.start();
	}

	// 显示送鲜花成功的对话框
	public void showFlowerSuccessDialog(int count) {
		showSuccessDialog(R.string.showmap_detail_activity_msg,
				R.string.showmap_detail_activity_sendflower_success, count);
	}

	// 显示送鲜花失败的对话框
	public void showFlowerFailDialog() {
		showFailDialog(R.string.showmap_detail_activity_msg,
				R.string.showmap_detail_activity_sendflower_fail);
	}

	// 显示送鸡蛋失败对话框
	public void showSendEggSuccessDialog(int count) {
		showSuccessDialog(R.string.showmap_detail_activity_msg,
				R.string.showmap_detail_activity_sendegg_success, count);
	}

	// 显示送臭鸡蛋失败的对话框
	public void showSendEggFailDialog() {
		showFailDialog(R.string.showmap_detail_activity_msg,
				R.string.showmap_detail_activity_sendegg_fail);
	}

	// 显示成功的对话框
	private void showSuccessDialog(final int title, final int msg,
			final int count) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				final AlertDialog alert = builder.create();
				alert.setTitle(title);
				alert.setMessage(context.getText(msg));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (callBack != null) {
									callBack.update(count);
								}
							}
						});
				alert.show();
			}
		});
	}

	// 显示失败的对话框
	private void showFailDialog(final int title, final int msg) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				final AlertDialog alert = builder.create();
				alert.setTitle(title);
				alert.setMessage(context.getText(msg));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alert.show();
			}
		});
	}
}
